package com.example.peanutsubway.web;

import com.alibaba.fastjson.JSONObject;
import com.example.peanutsubway.annotation.ResponseTypeAnnotation;
import com.example.peanutsubway.entity.AdminUserinfo;
import com.example.peanutsubway.entity.MenuInfo;
import com.example.peanutsubway.myEnum.ResponseEnum;
import com.example.peanutsubway.service.AdminUserService;
import com.example.peanutsubway.service.MenuInfoService;
import com.example.peanutsubway.service.impl.AdminUserServiceImpl;
import com.example.peanutsubway.service.impl.MenuInfoServiceImpl;
import com.example.peanutsubway.vo.UserInfo;

import javax.servlet.annotation.WebServlet;
import java.util.List;

/**
 * @Author：liulei
 * @Version：1.0
 * @Date：2021/10/11-16:54
 * @Since:jdk1.8
 * @Description:
 */
@WebServlet("/login.do")
public class LoginServlet extends BaseServlet<AdminUserinfo> { // <类类型>：将rquest中的请求参数（req.getParaer）封装为当前的类的类型的对象

    private AdminUserService adminUserService = new AdminUserServiceImpl();
    private MenuInfoService menuInfoService = new MenuInfoServiceImpl();

    @ResponseTypeAnnotation
    public String login(AdminUserinfo userinfo) {
        // 查询是否有当前用户
        String imgCode = request.getParameter("imgCode");
        if (request.getSession().getAttribute("trueImgCode").equals(imgCode)) {
            // 将login对象存储在session
            UserInfo login = adminUserService.login(userinfo.getUsername(), userinfo.getPwd());

            if (login!=null){
                session.setAttribute("login", login);
                // 查询用户的菜单信息，通过用户Id查询用户的菜单
                //  用户、菜单  ==》  角色Id   ==》 权限
                List<MenuInfo> menuInfos = menuInfoService.showUserMenuByRoleId(login.getRoleid());
                String json = JSONObject.toJSONString(menuInfos);
                session.setAttribute("menus", json);
                return "登录成功";
            }else {
                return "登录失败,账号或密码错误";
            }
        } else {
            return "验证码错误";
        }
    }
    @ResponseTypeAnnotation(type = ResponseEnum.REDIRECT)
    public String deleteLogin() {
        session.invalidate();
        return "/page/login.jsp";
    }
    @ResponseTypeAnnotation
    public String updateRoleMessage(AdminUserinfo adminUserinfo){
        Boolean aBoolean = adminUserService.updateRoleMessage(adminUserinfo);
        return aBoolean?"修改成功":"修改失败";
    }
}
